package com.jumi.microservice.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jumi.microservice.common.base.dto.BasePageResponse;
import com.jumi.microservice.common.base.dto.ResponseResult;
import com.jumi.microservice.common.utils.ResultGenerator;
import com.jumi.microservice.constant.ExceptionEnum;
import com.jumi.microservice.domain.Message;
import com.jumi.microservice.domain.vo.MessageUnreadVO;
import com.jumi.microservice.domain.vo.MessageVO;
import com.jumi.microservice.service.MessageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@Api(tags = "消息提醒相关接口")
@RequestMapping("/auth/Message")
public class MessageController {

    @Autowired
    private MessageService messageService;

    @ApiOperation(value = "获取用户消息中心未读数量", notes = "获取用户消息中心未读数量")
    @GetMapping("/getEverySystemNewNum")
    public ResponseResult<MessageUnreadVO> getEverySystemNewNum(Long uids){
        MessageUnreadVO vo = messageService.SystemNewNum(uids);
        return ResultGenerator.genSuccessResult(vo);
    }


    @ApiOperation(value = "获取消息中各种状态列表", notes = "获取消息中各种状态列表")
    @GetMapping("/mysystemmsg")
    public BasePageResponse mysystemmsg(
            @RequestParam(defaultValue = "0") Integer page,
            @RequestParam(defaultValue = "0") Integer size,
            Long uids,
            String type){
        LambdaQueryWrapper<Message> wrapper = Wrappers.lambdaQuery();
        switch (type) {
            case "sys":
                wrapper.in(Message::getSystemType,"6,7,8,9");
                break;
            case "fahuo":
                wrapper.eq(Message::getSystemType,1);
                break;
            case "fukuan":
                wrapper.eq(Message::getSystemType,2);
                break;
            case "yue":
                wrapper.eq(Message::getSystemType,3);
                break;
            case "tui":
                wrapper.eq(Message::getSystemType,4);
                break;
        }
        wrapper.eq(Message::getMessageType,1);
        wrapper.eq(Message::getToMemberId,uids);
        wrapper.orderByDesc(Message::getMessageId);
        IPage<Message> pageInfo = messageService.page(new Page<>(page, size), wrapper);

        return ResultGenerator.genSuccessResultPage(pos2Vos(pageInfo.getRecords()), pageInfo.getTotal());
    }


    /**
     * do->vo
     * @param messages
     * @return
     */
    private List<MessageVO> pos2Vos(List<Message> messages) {
        List<MessageVO> pos = messages.stream().map(p -> {
            MessageVO vo = null;
            try {
                vo = p.clone(MessageVO.class);
            } catch (Exception e) {
                ExceptionEnum.FAIL_CHANGE.newException();
            }
            return vo;
        }).collect(Collectors.toList());
        return pos;
    }
}
